package Shopfashion;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class KiemTraTaiKhoan
 */
@WebServlet("/Kiem_Tra_Tai_Khoan")
public class KiemTraTaiKhoan extends HttpServlet {
	private Connection connection;
	public void init() throws ServletException{
	       try{
	    	   //"com.mysql.jdbc.Driver"
	           Class.forName("com.mysql.jdbc.Driver");
	           String dbURL = "jdbc:mysql://localhost/shopfashion";
	           String username = "root";
	           String password = "123456";
	           connection = DriverManager.getConnection(
	               dbURL, username, password);
	       }
	       catch(ClassNotFoundException e){
	           System.out.println("Database driver not found.");
	       }
	       catch(SQLException e){
	           System.out.println(
	             "Error opening the db connection: "
	               + e.getMessage());
	       }
	   }
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String user = request.getParameter("User");
        String pass =request.getParameter("Password");
            try {
				if(checkUser(user,pass)==true){
					response.sendRedirect("San_Pham.jsp");
				}else{
					out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 ");
					out.println("Transitional//EN\">\n");
				    out.println("<html><head><title>Access Denied!</title></head><body>");
				    out.println("Your user or password is not match ,please <a href='Register.jsp'>Register</a>");
				    out.print("</body></hmtl>");
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
       // } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
          //  System.out.print(e.getMessage());
          //}
	}

	public boolean checkUser(String user,String password) throws ClassNotFoundException, ServletException{
        boolean check = false;
        init();
        try {	
	            if(connection!=null){
		            String sql = "SELECT * FROM taikhoan WHERE userName='"+user+"' AND password='"+password+"'";
		            Statement st = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
		            ResultSet   rs = st.executeQuery(sql);
		            if(rs.next())
		                check = true;
		            else
		                check = false;
	            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return check;
    }

}
